package alg20241031;

public class Text {
    public int[] searchRange(int[] nums, int target) {
        int left = searchLeft(nums, target);
        int right = searchRight(nums, target);
        if(left == -1) return new int[]{-1, -1};
        return new int[]{left, right};
    }
    public static int searchLeft(int[] nums, int target){
        int left = 0;
        int right = nums.length-1;
        while(left < right){
            int mid = left + (right - left) / 2;
            if(nums[mid] < target){
                left = mid + 1;
            }else{
                right = mid;
            }
        }

        return nums[left] == target ? left : -1;
    }

    public static int searchRight(int[] nums, int target){
        int left = 0, right= nums.length-1;
        while(left < right){
            int mid = left + (right - left +1) / 2;
            if(nums[mid] <= target){
                left = mid;
            }else{
                right = mid - 1;
            }
        }
        return nums[left] == target ? left : -1;
    }
}
